sap.ui.define([
    "sap/ui/core/Control",
    "sap/ui/core/HTML",
], function (
    Control,
    HTML,
) {
    "use strict";

    return Control.extend("autofiror.reuse.groupfeed.control.FlexB1", {
        metadata: {
            properties: {
                itemId: { type: "string", defaultValue: "" },
                listObject: { type: "object", defaultValue: null },
                lessContent: { type: "string", defaultValue: "" },
                fullContent: { type: "string", defaultValue: "" },
                maxLength: { type: "int", defaultValue: 0 },
            },
            aggregations: {
                _html: { type: "sap.ui.core.HTML", multiple: false, visibility: "hidden" }
            },
        },

        onBeforeRendering() {
            this._addFullItemContent(this.getListObject(), "");
            let returnItem = this._addHTMLContent(this.getListObject(), "", 0);
            if (returnItem.returnIfOver) {
                let con = returnItem.returnContent;
                this.setAggregation("_html", new HTML({
                    content: "<span>" + con + "</span>" + 
                    "<button class='link' id=" 
                    + this.getItemId() + "showMore" + ">...更多</button>"
                    + "<button class='link' id=" + 
                    this.getItemId() + 
                    "showLess" + ">...更少</button></span>",
                }).addStyleClass("wrap"));
                this.setProperty("lessContent", con, true);
            } else {
                this.setAggregation("_html", new HTML({
                    content: returnItem.returnContent
                }).addStyleClass("wrap"));
            }
        },


        onAfterRendering: function () {
            let showMoreId = "#" + this.getItemId() + "showMore";
            $(showMoreId).click(() => { // 为按钮添加点击事件处理程序
                this.getAggregation("_html").setContent(
                    "<span>" + this.getFullContent() + "</span>"
                );
                $(showMoreId).hide();
                $(showLessId).show();
            });
            let showLessId = "#" + this.getItemId() + "showLess";
            $(showLessId).click(() => { // 为按钮添加点击事件处理程序
                this.getAggregation("_html").setContent(
                    "<span>" + this.getLessContent() + "</span>"
                );
                $(showLessId).hide();
                $(showMoreId).show();
            });
            $(showLessId).hide();
        },

        _addHTMLContent(sourceList, content, length) {
            let ifOver = false;
            sourceList.forEach(element => {
                if (!ifOver) {
                    if (element.type == "sap.m.Text" || element.type == "sap.m.ExpandableText") {
                        length = length + element.content.length;
                        if (length > this.getMaxLength()) {
                            content = content + "<span>" + element.content.slice(0, this.getMaxLength() - length + element.content.length) + "</span>";
                            ifOver = true;
                        } else {
                            content = content + "<span>" + element.content + "</span>";
                        }
                        
                    }
                    else if (element.type == "sap.m.Link") {
                        length = length + element.content.length;
                        if (length > this.getMaxLength()) {
                            content = content + "<a class='link'>" + element.content.slice(0, this.getMaxLength() - length + element.content.length) + "</a>";
                            ifOver = true;
                        } else {
                            content = content + "<a class='link'>" + element.content + "</a>";
                        }
                    }
                }
            });
            return {
                returnContent: content,
                returnLength: length,
                returnIfOver: ifOver
            };
        },

        

        _addFullItemContent(sourceList, content) {
            sourceList.forEach(element => {
                if (element.type == "sap.m.Text" || element.type == "sap.m.ExpandableText") {
                    content = content + "<span>" + element.content + "</span>";
                }
                else if (element.type == "sap.m.Link") {
                    content = content + "<a class='link'>" + element.content + "</a>";
                }
            });
            this.setProperty("fullContent", content, true);
        },

        renderer(oRm, oControl) {
            // 此处的参数onControl指的就是该Control组件
            oRm.openStart("div", oControl);
            // 创建一个div
            oRm.openEnd();
            oRm.renderControl(oControl.getAggregation("_html"));
            oRm.close("div");
            // 闭合div
        }
    });
});